#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import time
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import RequestBase


class REDFISH_UID_003(Case):
    """
        Name:
            REDFISH_UID_003
        Label:
            UID操作设置闪烁点灯
        PreCondition:
            BMC web service normal
        TestStep:
            1.patch https://BMC_IP/redfish/v1/Chassis/1，body里添加{"IndicatorLED":"Blinking","Duration":50}，根据get获取If-Match: ETag_value
        ExpectedResult:
            1.执行响应码为200,uid灯闪烁50s
        Parameters:

        Author:
            baixin liuya
        Last Modified:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_pwd = self.bmc_host.password

        self.logger.info("获取Token")
        self.request = RequestBase(self.bmc_ip, self.bmc_user, self.bmc_pwd)
        return_code, session_id = self.request.create_session()
        self.assertIsNotNone(return_code)
        self.session_id = session_id

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 获取UID信息")
        url = "/redfish/v1/Chassis/1"
        header, rc, content = self.request.get_header_detail(url)
        if rc != 200:
            self.assertIsNotNone(None, f"获取UID信息失败：{json.dumps(content, indent=4)}")
        self.assertIsNotNone(header)
        if_match_str = header['ETag']
        self.request.headers['If-Match'] = if_match_str
        self.logger.info(json.dumps(content, indent=4))
        self.logger.info(f"UID灯状态：{content['IndicatorLED']}")

        self.log_step(f"Step2: 修改UID信息，使之UID灯闪烁")
        json_text = {"IndicatorLED": "Blinking", "Duration": 50}
        resp = self.request.patch(url=url, params=json_text)

        rc = resp[0]
        self.assertIsNotNone(rc)
        content = resp[1]
        self.logger.info(f"修改成功后：\n{json.dumps(content, indent=4)}")
        self.logger.info(f"UID灯状态：{resp[1]['IndicatorLED']}")
        time.sleep(3)

        self.log_step("Step3: 恢复UID灯")
        header = self.request.get_header(url)
        self.assertIsNotNone(header)
        if_match_str = header['ETag']
        self.request.headers['If-Match'] = if_match_str

        json_text = {"IndicatorLED": "Off"}
        resp = self.request.patch(url=url, params=json_text)

        rc = resp[0]
        self.assertIsNotNone(rc)
        content = resp[1]
        self.logger.info(f"恢复成功后：\n{json.dumps(content, indent=4)}")
        self.logger.info(f"UID灯状态：{content['IndicatorLED']}")
        time.sleep(3)


    # 测试结束后恢复环境
    def post_test_case(self):
        resp = self.request.delete_session(self.session_id)
        self.assertIsNotNone(resp[0], "清除Token失败")
        self.logger.info("清除Token成功")
